using Microsoft.VisualStudio.TestTools.UnitTesting;
using StockholmsStad.Data.Interfaces;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Data.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestUnitCouple : TestUnitBase
    {
        // before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            BeginTransaction();
        }

        // after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            EndTransaction();
        }

        [TestMethod]
        public void InsertAndRetrieveCoupleWithoutContact()
        {
            ICoupleAgent agent = AgentFactory.GetCoupleAgent();
            Couple couple = TestEntity.GetNewCoupleWithoutContact();
            int coupleId = agent.AddCouple(context, couple);
            couple.CoupleId = coupleId;

            Assert.AreNotEqual(0, coupleId, "Couple added error");

            Couple retrievedCouple = agent.GetCouple(context, coupleId);

            // Assign added id´s
            couple.Person1.PersonId = retrievedCouple.Person1.PersonId;
            couple.Person2.PersonId = retrievedCouple.Person2.PersonId;

            TestUnitPerson.ArePersonEqual(couple.Person1, retrievedCouple.Person1);
            TestUnitPerson.ArePersonEqual(couple.Person2, retrievedCouple.Person2);
            Assert.AreEqual(null, couple.ContactPerson);
        }

        [TestMethod]
        public void InsertAndRetrieveCoupleWithContact()
        {
            ICoupleAgent agent = AgentFactory.GetCoupleAgent();
            Couple couple = TestEntity.GetNewCoupleWithContact();
            int coupleId = agent.AddCouple(context, couple);
            couple.CoupleId = coupleId;

            Assert.AreNotEqual(0, coupleId, "Couple added error");

            Couple retrievedCouple = agent.GetCouple(context, coupleId);

            // Assign added id´s
            couple.Person1.PersonId = retrievedCouple.Person1.PersonId;
            couple.Person2.PersonId = retrievedCouple.Person2.PersonId;

            TestUnitPerson.ArePersonEqual(couple.Person1, retrievedCouple.Person1);
            TestUnitPerson.ArePersonEqual(couple.Person2, retrievedCouple.Person2);
            TestUnitPerson.AreContactEqual(couple.ContactPerson, retrievedCouple.ContactPerson);
        }


        [TestMethod]
        public void InsertUpdateAndRetrieveCouple()
        {
            ICoupleAgent agent = AgentFactory.GetCoupleAgent();
            Couple couple = TestEntity.GetNewCoupleWithoutContact();
            int coupleId = agent.AddCouple(context, couple);
            couple.CoupleId = coupleId;

            Assert.AreNotEqual(0, coupleId, "Couple added error");

            Couple addedCouple = agent.GetCouple(context, coupleId);

            Assert.IsNotNull(addedCouple, "Couple object null");

            Couple updatedCouple = couple;
            updatedCouple.Person1.FirstNames = "NewFirstName";
            updatedCouple.Person1.LastName = "NewLastName";

            updatedCouple.CoupleId = addedCouple.CoupleId;
            updatedCouple.Person1.PersonId = addedCouple.Person1.PersonId;
            updatedCouple.Person2.PersonId = addedCouple.Person2.PersonId;

            agent.UpdateCouple(context, coupleId, updatedCouple);

            Couple retrievedUpdatedCouple = agent.GetCouple(context, coupleId);

            // Assign added id´s
            updatedCouple.Person1.PersonId = retrievedUpdatedCouple.Person1.PersonId;
            updatedCouple.Person2.PersonId = retrievedUpdatedCouple.Person2.PersonId;

            TestUnitPerson.ArePersonEqual(updatedCouple.Person1, retrievedUpdatedCouple.Person1);
            TestUnitPerson.ArePersonEqual(updatedCouple.Person2, retrievedUpdatedCouple.Person2);
        }

        [TestMethod]
        public void UpdateCouple()
        {
            ICoupleAgent agent = AgentFactory.GetCoupleAgent();
            Couple couple = TestEntity.GetNewCoupleWithoutContact();

            // Add without couple
            int coupleId = agent.AddCouple(context, couple);
            couple.CoupleId = coupleId;
            Assert.AreNotEqual(0, coupleId, "Couple added error");

            couple = agent.GetCouple(context, coupleId);
            Assert.IsNull(couple.ContactPerson, "Contact object not null");

            // Add contact
            ContactPerson contactPerson = TestEntity.GetNewContactPerson();
            couple.ContactPerson = contactPerson;
            agent.UpdateCouple(context, coupleId, couple);
            couple = agent.GetCouple(context, coupleId);
            TestUnitPerson.AreContactEqual(contactPerson, couple.ContactPerson);

            // Update contact
            contactPerson.FirstNames = "Tralla la";
            couple.ContactPerson = contactPerson;
            agent.UpdateCouple(context, coupleId, couple);
            couple = agent.GetCouple(context, coupleId);
            TestUnitPerson.AreContactEqual(contactPerson, couple.ContactPerson);

            // Remove contact
            contactPerson = null;
            couple.ContactPerson = contactPerson;
            agent.UpdateCouple(context, coupleId, couple);
            couple = agent.GetCouple(context, coupleId);
            Assert.IsNull(couple.ContactPerson, "Contact object not null");

            // Contact is still null
            contactPerson = null;
            couple.ContactPerson = contactPerson;
            agent.UpdateCouple(context, coupleId, couple);
            couple = agent.GetCouple(context, coupleId);
            Assert.IsNull(couple.ContactPerson, "Contact object not null");
        }
    }
}